package com.fqgj.sentry.controller.pc;

import com.fqgj.sentry.biz.pc.ColumnBusiness;
import com.fqgj.sentry.biz.pc.vo.column.*;
import com.fqgj.sentry.common.annotations.VisitorAccessible;
import com.fqgj.sentry.common.cache.RequestLocalInfo;
import com.fqgj.sentry.common.response.ApiOutput;
import com.fqgj.sentry.common.utils.ApiPage;
import com.fqgj.sentry.controller.pc.request.AddColumnVo;
import com.fqgj.sentry.controller.pc.request.column.ColumnQueryVo;
import com.fqgj.sentry.controller.pc.request.UpdateColumnVo;
import com.fqgj.sentry.controller.pc.request.column.ColumnTypeQueryVo;
import com.fqgj.sentry.policy.enums.ColumnTypeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;

/**
 * Created by zwh @yuntu-inc.com
 *
 * @description
 * @create 2017/7/29 0029 上午 11:37
 */
@RestController
@RequestMapping(value = "/column")
public class ColumnController {
    @Autowired
    private ColumnBusiness columnBusiness;


    /**
     * 查询字段列表
     *
     * @param eventId
     * @return
     */
    @VisitorAccessible
    @RequestMapping(value = "/{eventId}/need/list", method = RequestMethod.POST)
    public ApiOutput findPolicySetsNeedColumnList(@PathVariable Long eventId) {
        List<NeedColumnVo> columnVoList = columnBusiness.findPolicySetsNeedColumnList(eventId);
        return new ApiOutput<>("list", columnVoList);
    }


    /**
     * 字段新增
     *
     * @param addColumnVo
     * @return
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public ApiOutput saveColumn(@RequestBody AddColumnVo addColumnVo) {
        columnBusiness.saveColumn(RequestLocalInfo.getCurrentMerchantId(), addColumnVo);
        return new ApiOutput<>();
    }

    /**
     * 字段更新
     *
     * @param updateColumnVo
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public ApiOutput updateColumn(@RequestBody UpdateColumnVo updateColumnVo) {
        columnBusiness.updateColumn(updateColumnVo);
        return new ApiOutput<>();
    }

    /**
     * 字段删除
     *
     * @param columnId
     * @return
     */
    @RequestMapping(value = "/{columnId}/delete", method = RequestMethod.POST)
    public ApiOutput deleteColumn(@PathVariable Long columnId) {
        columnBusiness.deleteColumn(columnId);
        return new ApiOutput<>();
    }

    /**
     * 查询字段列表
     *
     * @param columnQueryVo
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public ApiOutput listColumn(@RequestBody ColumnQueryVo columnQueryVo) {
        ApiPage apiPage = columnBusiness.listColumn(RequestLocalInfo.getCurrentMerchantId(), columnQueryVo);
        return new ApiOutput<>(apiPage);
    }

    /**
     * 查询字段列表
     *
     * @param policySetsId
     * @return
     */
    @RequestMapping(value = "/{policySetsId}/list", method = RequestMethod.POST)
    public ApiOutput findListByPolicySetsId(@PathVariable Long policySetsId) {
        List<PolicyColumnVo> columnVoList = columnBusiness.findListByPolicySetsId
                (RequestLocalInfo.getCurrentMerchantId(), policySetsId, ColumnTypeEnum.field_function.getType());
        return new ApiOutput<>("list", columnVoList);
    }


    /**
     * 查询字段列表
     *
     * @param columnTypeQueryVo
     * @return
     */
    @RequestMapping(value = "/event/relation/list", method = RequestMethod.POST)
    public ApiOutput findRelationList(@RequestBody ColumnTypeQueryVo columnTypeQueryVo) {
        List<PolicyColumnVo> columnVoList = columnBusiness.findEventRelationList
                (RequestLocalInfo.getCurrentMerchantId(), columnTypeQueryVo);
        return new ApiOutput<>("list", columnVoList);
    }


    /**
     * 函数详情
     *
     * @param functionId
     * @return
     */
    @RequestMapping(value = "/{functionId}/info", method = RequestMethod.POST)
    public ApiOutput findFunctionInfo(@PathVariable Long functionId) {
        FunctionVo functionVo = columnBusiness.findFunctionInfo(functionId);
        return new ApiOutput<>(functionVo);
    }


    /**
     * 函数详情
     *
     * @param functionVo
     * @return
     */
    @RequestMapping(value = "/{functionId}/update", method = RequestMethod.POST)
    public ApiOutput updateFunctionInfo(@RequestBody FunctionVo functionVo) {
        columnBusiness.updateFunctionInfo(functionVo);
        return new ApiOutput<>(functionVo);
    }


    /**
     * 函数详情
     *
     * @param testFunctionVo
     * @return
     */
    @RequestMapping(value = "/{functionId}/test", method = RequestMethod.POST)
    public ApiOutput testFunctionDsl(@RequestBody TestFunctionVo testFunctionVo) {
        String result = columnBusiness.testFunctionDsl(testFunctionVo);
        return new ApiOutput<>(result);
    }

    /**
     * 操作列表
     *
     * @param
     * @return
     */
    @RequestMapping(value = "/function/operate/list", method = RequestMethod.POST)
    public ApiOutput functionOperateList() {
        List<FunctionOperateVo> list = columnBusiness.functionOperateList();
        return new ApiOutput<>("list", list);
    }


}
